<?php

class Medal_model extends CI_Model {

    function MedalModel() {
        parent::Model();
    }

    function checkExistingOMedal() {
        $return = array();
        $query = $this->db->get_where('Medal', array('Medal_Type_ID' => check_post('medaltype'), 'Medal_Class_ID' => check_post('medalclass')));
        if (!$query->num_rows) {
            $q1 = $this->db->query("SELECT name FROM Medal_Type WHERE ID = " . check_post('medaltype'));
            $q2 = $this->db->query("SELECT name FROM Medal_Class WHERE ID = " . check_post('medalclass'));
            $row1 = $q1->row_array();
            $row2 = $q2->row_array();
            $this->db->select_max('[order]', 'max_order');
            $query = $this->db->get('Medal');
            $max_order = $query->result_array();
            $order = (($query->num_rows > 0) ? $max_order[0]['max_order'] + 1 : 1);
            $this->db->insert('Medal', array(
                'Medal_Type_ID' => check_post('medaltype'),
                'Medal_Class_ID' => check_post('medalclass'),
                'full_name' => " ",
                'full_name' => "{$row1['name']}/{$row2['name']}",
                '[order]' => $order
            ));
            $insertID = $this->db->insert_id();
        } else {
            $result = $query->result_array();
            $insertID = $result[0]['ID'];
        }
        return $insertID;
    }

    function getMedalTypeClass() {
        $this->db->select("Medal.ID, Medal.full_name name");
        $query = $this->db->get('Medal');
        return $query->result_array();
    }

    function getMedal($condition = "") {
        $q = "SELECT ID , full_name  FROM Medal ";
        if ($condition != "") {
            $q .= " WHERE full_name LIKE '%$condition%' LIMIT 300";
        }
        $query = $this->db->query($q);
        return $query->result_array();
    }

}

?>
